#cmake file for project erpcdemo
#author:guyadong
#created:2020/04/14
# 指定对cmake的最低版本要求
cmake_minimum_required( VERSION 3.0 )
cmake_policy(SET CMP0048 NEW)
# 3.0以上版本才允许使用VERSION option
# 定义项目名称以及语句类型为C CXX混合
project(erpcdemo VERSION 0.0.0 LANGUAGES C CXX)

# 不支持Windows平台,否则报错
if(WIN32)
	message(STATUS "UNSUPPORTED PLATFORM,WIN32,linux required")
endif(WIN32)

#判断编译类型和版本是否满足编译要求
if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 5)
	message(STATUS "CMAKE_CXX_COMPILER: ${CMAKE_CXX_COMPILER}") 
	message(STATUS "CMAKE_CXX_COMPILER_VERSION=${CMAKE_CXX_COMPILER_VERSION}" )
else()
	message(FATAL_ERROR "compiler required: gcc 5.2.0 or above" )
endif()
# includes utils.cmake module
# CMAKE_MODULE_PATH 是cmake的内置变量,参见cmake用户文档 https://cmake.org/cmake/help/v3.5/variable/CMAKE_MODULE_PATH.html
list (APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/Modules")
include (utils)
# 增加C++11支持选项,此函数定义在${PROJECT_SOURCE_DIR}/cmake/Modules/utils.cmake
cxx11_support()

# 定义erpddemo库名,生成的库名为liberpcdemo_client.a(gcc),erpcdemo_client.lib(msvc)
set(LRC_LIBNAME erpcdemo_client)
set(LRS_LIBNAME erpcdemo_server)
# 定义erpddemo包名(用于find_package查找本项目的名字)
set(RD_PKGNAME erpcdemo) 

##############设置目标文件生成位置#####################
# CMAKE_前缀的变量都是cmaek内置的变量,cmake参见文档:https://cmake.org/cmake/help/v3.5/manual/cmake-variables.7.html
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")
# define RUNTIME_INSTALL_DIR LIBRARY_INSTALL_DIR INCLUDE_INSTALL_DIR CONFIG_INSTALL_DIR
set (RUNTIME_INSTALL_DIR bin)
set (LIBRARY_INSTALL_DIR lib)
set (INCLUDE_INSTALL_DIR include)
set (CONFIG_INSTALL_DIR  ${LIBRARY_INSTALL_DIR}/cmake/erpcdemo)

# MODULE方式查找依赖库,参见${PROJECT_SOURCE_DIR}/cmake/Modules/FindErpc.cmake
find_package(Erpc MODULE REQUIRED)

############ 创建 erpcdemo_client #################
add_library(erpcdemo_client STATIC erpcdemo_client.cpp)
# 指定library对外的include文件夹
# $<> 格式的表达式叫cmake-generator-expressions,参见cmake用户手册: https://cmake.org/cmake/help/v3.5/manual/cmake-generator-expressions.7.html
target_include_directories (erpcdemo_client 
  INTERFACE "$<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>"
			"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
			"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>"
	)
# 设置library属性	
# PUBLIC_HEADER DEBUG_POSTFIX都是library内置属性(property)，参见cmake用户手册:https://cmake.org/cmake/help/v3.5/manual/cmake-properties.7.html
# 将lock.h指定为对外公开的header
# 指定生成debug版的库文件的后缀为'_d',以与release版区分
# 即生成debug版的库名为liberpcdemo_client_d.a(gcc),erpcdemo_client_d.lib(msvc)
set_target_properties (${target}  PROPERTIES 
	PUBLIC_HEADER lock.h
  	DEBUG_POSTFIX _d
	)
# 指定依赖库为 erprc	
target_link_libraries(erpcdemo_client erpc)
#set_target_properties(erpcdemo_client  PROPERTIES INTERFACE_POSITION_INDEPENDENT_CODE ON)
############ 创建 erpcdemo_server #################
add_library(erpcdemo_server STATIC erpcdemo_server.cpp erpcdemo_server_impl.cpp)
target_include_directories (erpcdemo_server 
INTERFACE "$<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>"
			"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
			"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..>"
	)
set_target_properties (${target}  PROPERTIES 
	PUBLIC_HEADER "erpcdemo.h erpcdemo_server.h"
  	DEBUG_POSTFIX _d
	)
target_link_libraries(erpcdemo_server erpc)
#set_target_properties(erpcdemo_server  PROPERTIES INTERFACE_POSITION_INDEPENDENT_CODE ON)
############ 创建client测试 test_erpcdemo_client #################
add_executable(test_erpcdemo_client test_erpcdemo_client.cpp erpc_setup_tcp.cpp)
target_link_libraries(test_erpcdemo_client erpcdemo_client)
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
	target_link_libraries(test_erpcdemo_client -pthread -ldl)
endif()
############ 创建client测试 test_erpcdemo_server #################
add_executable(test_erpcdemo_server test_erpcdemo_server.cpp erpc_setup_tcp.cpp)
target_link_libraries(test_erpcdemo_server erpcdemo_server)
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
	target_link_libraries(test_erpcdemo_server -pthread -ldl)
endif()

#########################
##########安装配置脚本###############
include (CMakePackageConfigHelpers)
set (lr_DEPENDENCY "find_dependency(erpc)")
configure_package_config_file (${CMAKE_CURRENT_LIST_DIR}/cmake/config.cmake.in
  ${CMAKE_CURRENT_BINARY_DIR}/${RD_PKGNAME}-config.cmake
  INSTALL_DESTINATION ${CONFIG_INSTALL_DIR}
  NO_CHECK_REQUIRED_COMPONENTS_MACRO)
write_basic_package_version_file (${RD_PKGNAME}-config-version.cmake VERSION
  ${PROJECT_VERSION} COMPATIBILITY SameMajorVersion)
########安装脚本##########
install(TARGETS erpcdemo_client erpcdemo_server EXPORT ${RD_PKGNAME}-targets
  RUNTIME DESTINATION ${RUNTIME_INSTALL_DIR}
  LIBRARY DESTINATION ${LIBRARY_INSTALL_DIR}
  ARCHIVE DESTINATION ${LIBRARY_INSTALL_DIR}
  PUBLIC_HEADER DESTINATION ${INCLUDE_INSTALL_DIR}/${RD_PKGNAME}
  INCLUDES DESTINATION ${INCLUDE_INSTALL_DIR}/${RD_PKGNAME}
  )

install (FILES
  ${CMAKE_CURRENT_BINARY_DIR}/${RD_PKGNAME}-config.cmake
  ${CMAKE_CURRENT_BINARY_DIR}/${RD_PKGNAME}-config-version.cmake
  DESTINATION ${CONFIG_INSTALL_DIR}
  )
install(EXPORT ${RD_PKGNAME}-targets DESTINATION ${CONFIG_INSTALL_DIR})
